package GameLogic
{
	import DataStore.BmpDataPool;
	import fl.controls.Button;
	import fl.controls.CheckBox;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.media.Video;
	import flash.net.FileReference;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.utils.Dictionary;
	import RobotPlayer.RobotFactory;

	public class LuaEditer extends Sprite
	{
		public  var Hide:Boolean = true;
		public  var Pos:Point = new Point(0,0);
		public  var btn:Button;
		public  var runImgBtn:SpriteBtn;
		public  var clearImgBtn:SpriteBtn;
		public  var traceImgBtn:SpriteBtn;
		public  var closeImgBtn:SpriteBtn;
		public  var LuaEditBtn:SpriteBtn;
		public  var input:TextArea;		
		public  var OutputWin:TextArea;
		public  var CircleRun:CheckBox;
		public  var ApiChoise:CheckBox;
		public  var CurrentapiEdit:String;
		
		private var beforHelp:String = "";
		private var ShowHelp:Boolean = false;
		
		private var sideTo:TextField;
		private var closewin:TextField;
		private var sideRight:Boolean = true;
		
		private var initX:int;
		private var initY:int;
		
		public function LuaEditer()
		{
			Init();
			Game.MenuCanvas.addChild(this);
		}
		
		public  function Init():void
		{
			this.x = 154;
			this.y = 40;
			initX = 154;
			initY = 40;
			this.alpha = 0.9;
			Delay();
		}
		
		public function PutChoiseApi(_remark:String,_api:String):void
		{
			input.text = "";
			input.text = Game.LuaScriptApiSt[_api];
		}
		
		private function Delay():void
		{
			var txt:TextField = new TextField();
			txt.text = ">>循环逻辑 ";
			txt.textColor = 0xffffff;
			txt.background = true;
			txt.border = true;
			txt.borderColor = 0x33cc33;
			txt.backgroundColor = 0x003300;
			txt.x = 5;
			txt.y = 218;
			txt.width = 85;
			txt.height = 24;
			CircleRun = new CheckBox();
			CircleRun.x = 69;
			CircleRun.y = 218;
			CircleRun.label = "";
			var  note:String = "勾选后，不必点击运行按钮,这里面的语句会直接在机器人循环逻辑中不断被执行,只针对玩家机器人,需要注意这里的执行效率";
			txt.addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void
			{
				Game.AddTooltip(Game.LuaEdit,CircleRun, note, -150, -24);
			});
			txt.addEventListener(MouseEvent.MOUSE_OUT, function(e:Event):void
			{
				Game.RemoveTooltip();
			});
						
			var top11:Bitmap = new Bitmap(BmpDataPool.LuaeditPool[4049]);
			addChild(top11);
			var leff11:Bitmap = new Bitmap(BmpDataPool.LuaeditPool[4052]);
			addChild(leff11);
			leff11.y = top11.height;
			
			var bg:Shape =  new Shape(); 
			bg.graphics.beginFill(0x000000, 1);
			bg.graphics.drawRect(5, 39, 593, 30);
			bg.graphics.beginFill(0xffffff, 1);
			bg.graphics.drawRect(236, 69, 363, 449);
			addChild(bg);
			
			var closeBtn:ImgBtn = new ImgBtn(4012,"退出");
			closeBtn.Addclick(function():void
			{
				Game.LuaEdit.visible = false;
				Game.SetButton.SettingPanel.radar.selected = Game.InitShowRadar;
				Game.InfoShowBtn.Check = !Game.InfoShowBtn.Check;
				Game.InfoShowBtn.ConsistentIco();
			});
			closeBtn.y = 40;
			closeBtn.x = 10;
			
			var startX:int = 260;
			var spaceX:int = 50;
			var homeBtn:ImgBtn = new ImgBtn(4007,"主 页");
			homeBtn.Addclick(function():void
			{
				if (!ShowHelp)
				{
				   if(input.text.length > 0)
				        beforHelp = input.text;
				   input.htmlText = DataSource.myText;
				   ShowHelp = true;
				}
				else
				{
					ShowHelp = false;
					input.htmlText = "";
					input.text = beforHelp;
				}
			});
			homeBtn.y = 40;
			homeBtn.x = startX;
			
			var helpBtn :ImgBtn = new ImgBtn(4004,"帮助文档");
			helpBtn.Addclick(function():void
			{
				if (!ShowHelp)
				{
				   if(input.text.length > 0)
				        beforHelp = input.text;
				   input.htmlText = DataSource.myText;
				   ShowHelp = true;
				}
				else
				{
					ShowHelp = false;
					input.htmlText = "";
					input.text = beforHelp;
				}
			});
			helpBtn.y = 40;
			helpBtn.x = startX + 1*spaceX;
			
			var playBtn:ImgBtn = new ImgBtn(4010,"执行脚本");
			playBtn.Addclick(function():void
			{
				if (ApiChoise.selected)
				{
					Game.LuaScriptApiSt[Game.LuaEdit.CurrentapiEdit] = input.text;
				}
				else 
				   DoLuaScript();
			});
			playBtn.y = 40;
			playBtn.x = startX + 2*spaceX;
			
			//var pauseBtn:ImgBtn = new ImgBtn(4009,"暂停脚本执行");
			//pauseBtn.Addclick(function():void
			//{
				//trace("ok");
			//});
			//pauseBtn.y = 40;
			//pauseBtn.x = startX + 3*spaceX;
			
			var stopBtn:ImgBtn = new ImgBtn(4008,"清除脚本");
			stopBtn.Addclick(function():void
			{
				input.text = "";
				input.htmlText = "";
			});
			stopBtn.y = 40;
			stopBtn.x = startX + 3*spaceX;
			
			var saveBtn:ImgBtn = new ImgBtn(4013,"保存脚本");
			saveBtn.Addclick(function():void
			{
				var luafile:FileReference = new FileReference();
				luafile.save(input.text, null);
			});
			saveBtn.y = 40;
			saveBtn.x = startX + 4*spaceX;
			
			//var importBtn:ImgBtn = new ImgBtn(4005,"导入脚本");
			//importBtn.Addclick(function():void
			//{
				//trace("ok");
			//});
			//importBtn.y = 40;
			//importBtn.x = startX + 6*spaceX;
			
			//var setBtn:ImgBtn = new ImgBtn(4014,"设 置");
			//setBtn.Addclick(function():void
			//{
				//trace("ok");
			//});
			//setBtn.y = 40;
			//setBtn.x = startX + 7*spaceX;
			
			var reBtn:ImgBtn = new ImgBtn(4003,"输出窗口");
			reBtn.Addclick(function():void
			{
				OutputWin.visible = !OutputWin.visible;
				sideTo.visible = !sideTo.visible;
				closewin.visible = !closewin.visible;
			});
			reBtn.y = 40;
			reBtn.x = startX + 5*spaceX;
			
			
			var clearBtn:ImgBtn = new ImgBtn(4006,"清屏窗口");
			clearBtn.Addclick(function():void
			{
				OutputWin.text = ">>";
			});
			clearBtn.y = 40;
			clearBtn.x = startX + 6*spaceX;
			
			

			input = new TextArea();
			input.x = 125;
			input.y = 78;
			input.width = 470;
			input.height = 420;
			//input.htmlText = RobotFactory.myText;
			var tf:TextFormat = new TextFormat();
			tf.size = 14;
			//tf.color =  0xffffff;//0x4A4646;//
			input.textField.background = true;
			input.textField.backgroundColor = 0xF7F7F7;//0x25028C;
			input.setStyle("textFormat", tf);
	
			var tf1:TextFormat = new TextFormat();
			tf1.size = 12;
			var tt:TextField = new TextField();
			tt.wordWrap = true;
			tt.text = "     LuaRobot 是独立开发的机器人AI仿真引擎。与通常不同：利用对其进行编程，设计AI智能\n\n\n\n\n来自动指挥它，同时也是一款学习Lua脚本的好工具。\n\n LuaRobot是一种有趣的学习性编程，使用几行简单的代码，就能够让你创建一个活生生的机器人，作为对游戏的编程学习，由浅入深，直观上手，了解游戏AI的一切奥秘。";
			tt.setTextFormat(tf1);
			//addChild(tt);
			tt.x = 10;
			tt.y = 137;
			tt.background = true;
			tt.alpha = 0.3;
			tt.width = 110;
			tt.height = 430;
			tt.selectable = false;
			this.addChild(input);
			
			var tf3:TextFormat = new TextFormat();
			tf3.size = 11;
			tf3.color = 0x3400C8;
			var em:TextField = new TextField();
			em.selectable = false;
			em.text = "------- 应用客服联系:QQ 474165841 刘振宁 ----- 2012.CopyRight -----";
			em.setTextFormat(tf3);
			//addChild(em);
			em.width = 400;
			em.height = 20;
			em.x = 59;
			em.y = 500;
			
			//addChild(importBtn);
			addChild(stopBtn);
			addChild(closeBtn);
			addChild(homeBtn);
			addChild(helpBtn);
			addChild(playBtn);
			//addChild(pauseBtn);
			addChild(reBtn);
			//addChild(setBtn);
			addChild(saveBtn);
			addChild(clearBtn);
			
			OutputWin = new TextArea();
			OutputWin.width = 245;
			OutputWin.height = 306;
			OutputWin.x = -120;
			OutputWin.y = 79;
			OutputWin.alpha = 1;
			
			sideTo =  new TextField();
			sideTo.text = "观察游戏界面";
			sideTo.background = true;
			sideTo.backgroundColor = 0x000000;
			sideTo.textColor = 0xffffff;
			sideTo.width = 75;
			sideTo.x = 50;
			sideTo.selectable = false;
			sideTo.height = 20;
			sideTo.addEventListener(MouseEvent.CLICK, function(e:Event):void
			{
				if (sideRight)
				{
				   SetSide(630, "恢复编辑界面");
				   sideRight = !sideRight;
				}
				else
				{
				   SetSide(initX, "观察游戏界面");
				   sideRight = !sideRight;
				}
			});
			sideTo.addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void
			{
				Mouse.cursor = MouseCursor.BUTTON;
				Game.AddTooltip(Game.LuaEdit, sideTo, "隐藏编辑器，保留输出窗口,游戏界面,便于观察", 80, 0);
			});
			sideTo.addEventListener(MouseEvent.MOUSE_OUT, function(e:Event):void
			{
				Game.RemoveTooltip();
				Mouse.cursor = MouseCursor.AUTO;
			});
			addChild(sideTo);
			sideTo.y = OutputWin.height + OutputWin.y;
			sideTo.visible = false;
			sideTo.selectable = false;
			
			closewin =  new TextField();
			closewin.text = "关闭窗口";
			closewin.background = true;
			closewin.backgroundColor = 0x000000;
			closewin.textColor = 0xffffff;
			closewin.width = 50;
			closewin.selectable = false;
			closewin.height = 20;
			closewin.addEventListener(MouseEvent.CLICK, function(e:Event):void
			{
				OutputWin.visible = false;
				sideTo.visible = false;
				closewin.visible = false;
				if (Game.LuaEdit.x != initX)
				{
				   Game.LuaEdit.x = initX;
				   SetSide(initX, "观察游戏界面");
				   sideRight = !sideRight;
				}
			});
			closewin.addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void
			{
				Mouse.cursor = MouseCursor.BUTTON;
			});
			closewin.addEventListener(MouseEvent.MOUSE_OUT, function(e:Event):void
			{
				Mouse.cursor = MouseCursor.AUTO;
				Game.RemoveTooltip();
			});
			addChild(closewin);
			closewin.y = OutputWin.height + OutputWin.y;
			closewin.x = sideTo.x - 60;
			closewin.visible = false;
			closewin.selectable = false;
			
			OutputWin.visible = false;		
			OutputWin.editable = false;
			
			addChild(txt);
			addChild(CircleRun);
			
			
			var h:ApiCheck = new ApiCheck("Lua 脚本方法列表");
			h.Add("OnMissileRaid()", "被导弹击中时触发事件","被导弹击中时触发事件,在这里编辑lua脚本，也可以调用系统提供方法,请参考具体Api说明",true);
			h.Add("OnCollisionWall()", "和墙发送碰撞时触发事件","和墙发送碰撞时触发事件");
			h.Add("OnRadarScan()", "雷达扫描到目标", "雷达扫描到目标，获知目标相对位置，方位角",true);
			h.Add("Force()", "动力输出", "这是一个重要的方法，输出一个有方向的力，才是机器人运动的根本原因");
			h.Add("c", "1","",true);
			h.Add("d", "1", "");
			h.Add("e", "1", "",true);
			h.Add("f", "1", "");
			h.Add("h", "1", "",true);
			h.Add("i", "1", "");
			h.Add("g", "1", "",true);
			h.Add("k", "1", "");
			h.Add("l", "1", "",true);
			h.x = -116;
			h.y = 80;
			h.BorderBg();
			h.visible = false;
			var txt1:TextField = new TextField();
			txt1.text = "选择具体函数 编辑脚本AI";
			addChild(txt1);
			txt1.textColor = 0xffffff;
			txt1.background = true;
			txt1.border = true;
			txt1.borderColor = 0x33cc33;
			txt1.backgroundColor = 0x003300;
			txt1.wordWrap = true;
			txt1.x = 5;
			txt1.y = 258;
			txt1.width = 84;
			txt1.height = 38;
			txt1.selectable = false;
			var  note1:String = "点击打开脚本功能列表，会针对相应的功能进行编程,完成编辑后，点击执行脚本，相应函数会保存并执行。注意:勾选后才能进行编辑";
			txt1.addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void
			{
				Game.AddTooltip(Game.LuaEdit,txt1, note1, -100, -22);
			});
			txt1.addEventListener(MouseEvent.MOUSE_OUT, function(e:Event):void
			{
				Game.RemoveTooltip();
			});
			txt1.addEventListener(MouseEvent.CLICK, function(e:Event):void
			{
				Game.RemoveTooltip();
				h.visible = true;
				ApiChoise.selected = true;
			});
			ApiChoise = new CheckBox();
			ApiChoise.x = 68;
			ApiChoise.y = 273;
			ApiChoise.label = "";
			
			addChild(ApiChoise);
			addChild(h);
			addChild(OutputWin);
		}
		
		
		private function DoLuaScript():void
		{
			try
			{
				var st:String = Game.LuaDict["InitScript"] +"\n";
			    Game.LuaAl.doString(st+input.text);
			}
			catch (e:Error)
			{
				trace(e.message);
			}
		}
		
		private function SetSide(_x:int,_note:String):void
		{
			this.x = _x;
			sideTo.text = _note;
		}
	}
}